package nathizm;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.util.AuthenticationException;

public class Blogger {

	protected static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
	protected static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
	protected static final String METAFEED_URL = "http://www.blogger.com/feeds/default/blogs";
	protected static final String FEED_URI = Blogger.FEED_URI_BASE + "/" + System.getProperty(Global.BLOG_ID_KEY);
	protected static final String AUTHOR_NAME = System.getProperty(Global.AUTHER_KEY);

	protected static BloggerService getService() throws AuthenticationException {
	
		BloggerService myService = new BloggerService("exampleCo-exampleApp-1");
		myService.useSsl();
		myService.setUserCredentials(System.getProperty(Global.USER_ID_KEY), System.getProperty(Global.USER_PASSWORD_KEY));
	
		return myService;
	}

	protected static void outputFile(Entry entry) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fname = sdf.format(new Date(entry.getPublished()
				.getValue()))
				+ "_"
				+ entry.getTitle().getPlainText()
				+ "_" + entry.getId().split("post-")[1] + ".txt";
		File outFile = new File(System.getProperty(Global.ENTRY_FOLDER_KEY), fname);
		FileWriter fw = new FileWriter(outFile);
		TextContent content = (TextContent)entry.getContent();
		HtmlTextConstruct html = (HtmlTextConstruct)content.getContent();
		fw.write(html.getHtml());
		fw.close();
		System.out.println(fname);
	}

}